Welcome back.
So I'm going to start by defining a class to represent a person.
So I'm going to say class Person.
And let's say that every person has a name and year that they were born.
So I'm going to create a constructor, def __ init.
That accepts a name and a year_born and
assigns instance variables for name and year_born.
Self.name = name.
Self.year_born = year_born.
Now let's suppose that I also want to have a method getAge.
So getAge that takes this year_born and
let's suppose that we have a current year.
So I'll define CURRENT_YEAR to be 2019.
So getAge is going to subtract CURRENT_YEAR.
So I'll say return CURRENT_YEAR- self.year_born.
And let's suppose that we also want to override the __str__ method,
so I'll say def __str__, to print out self.name.
So, I'll use the dot format method.
And .format self.name and
self.getAge.
Okay, so now we have a class to represent a person and
every person has a name and a year that they were born.
So if I create something like alice
equals a new person named Alice Smith.
And I print out the value of Alice, so
after passing the year_born so I'll just say 1990.
Oops, I need one more closing parenthesis here.
Okay, so now I can see that Alice Smith is the name,
and 29 is the age given that the year born is 1990.
Of course, we're just going to ignore things like exact birthdays for
this purpose.
Now, suppose that I wanted to define a new class to represent a student, and what I
want a student class to have is everything that a person has, plus one more thing.
So I want students to have this notion of knowledge.
So what I'm going to do, is I'm going to start by just copying and
pasting my person class.
So I can this, and then let's suppose that I want to have another instance variable,
named knowledge, which I'll start out as 0.
So I'll say, self.knowledge = 0.
And I want this to be student, instead of person.
And let's suppose that I want to have this other method on
a student named study, so def study.
And whenever a student studies, then their knowledge should go up,
so I'll say self.knowledge +=1 okay.
So, now if I define Alice to be a student instead of just a person.
By saying, alice equals a new student.
Then you can see that I still get Alice Smith, age 29.
If I print out alice.knowledge, then I should get 0.
If I tell alice to study, and then print out alice's
knowledge level, then I'm going to get 1.
Now, this is a fine way of defining both the person class and the stream class.
But there is a much easier in more concise way of actually doing this using
something called inheritance.
The idea of inheritance is that you can define classes that inherit from other
classes.
So, in other words, they take all of the instance variables,
all of the methods that that other class has, and
then they can add more instance variables and more methods.
So, in this case, every student is a person.
It's not true that necessarily every person is a student, but
every student is a person.
So, what I'm going to do is I'm going to say that this student class should
inherit from person.
So a student should have everything a person has plus more.
Now, the way that I say I want a student to inherit from person is by putting
parenthesis after the class declaration here.
And putting the class I want to inherit from inside of those parentheses.
So I'll say that every student is a person.
So now that I do this I can get rid of some of the repetitive parts
of the student class.
So here for example getAge, is a little bit repetitive.
I don't need getAge in both student and
person because getAge is already inside of person.
So I'm going to remove getAge from the student class.
I'm going to do the same for the def__ str__ method.
And I'm going to do one more thing as well.
So this setting self.name and self.year_born is a little bit repetitive
with the Person class setting self.name and self.year_born.
So in the constructor for the student,
I'm going to say that I want to call the constructor, this method, of Person.
And the way that I'll do that,
is I'll say Person.__init__, and
I'm going to pass in self, name, and year_born.
Now, when I do this I'm going to call this init method, or
the Person constructor, on this new student.
So this is going to call this method.
And then this method is going to set instance variables for
name and for year_born automatically.
So let's test this out with Alice, and be sure that our code still works.
So you can see again we have our student Alice Smith born in 1990.
And if I still run this code you can see that after Alice studies then Alice's
knowledge is 1.
But now if I print out alice.getAge.
You'll see that Alice is 29.
If I print out Alice, Then you'll see that we get Alice Smith, age 29.
And the important part here is that even though Alice is a student,
and even though the student class didn't directly define __str__ or getAge.
I'm able to call both of these methods on alice,
because alice is a Student, and Student inherits from Person, and
the Person class has these methods, getAge, and __str__.
That's all for now, until next time.